#include <unordered_map>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

unordered_map<int, vector<int>> getTaskIndices(const vector<int> &tasks) {
  unordered_map<int, vector<int>> records;

  for (int i = 0; i < tasks.size(); ++i) {
    if (records.find(tasks[i]) != records.end()) {
      records[tasks[i]].push_back(i);
    }
    else {
      records[tasks[i]] = {i};
    }
  }

  return records;
}

vector<vector<int>> solution(const vector<int> &tasks) {
  vector<int> tasks_copy = tasks;
  sort(tasks_copy.begin(), tasks_copy.end());
  vector<vector<int>> result;

  unordered_map<int, vector<int>> records = getTaskIndices(tasks);

  int l = 0, r = tasks.size() - 1;

  while (l < r) {
    int l_task = tasks_copy[l];
    int l_index = records[l_task].back();
    records[l_task].pop_back();

    int r_task = tasks_copy[r];
    int r_index = records[r_task].back();
    records[r_task].pop_back();

    result.push_back({l_index, r_index});
    ++l;
    --r;
  }

  return result;
}

int main(int argc, char const *argv[])
{
  /* code */
  vector<int> tasks {1, 3, 5, 3, 1, 4};

  for (auto v : solution(tasks)) {
    for (auto i : v) {
      cout << i << " ";
    }
    cout << endl;
  }
  return 0;
}
